<?php
$menumark = 'zsf';
include($pe['path_root'].'hook/zsf.hook.php');
$jidu = array('01'=>"一",'02'=>"一",'03'=>"一",'04'=>"二",'05'=>"二",'06'=>"二",'07'=>"三",'08'=>"三",'09'=>"三",'10'=>"四",'11'=>"四",'12'=>"四");
switch ($act) {
    //#####################@ 用户添加 @#####################//
    case 'add':
        if (isset($_p_pesubmit)){
            pe_token_match();
            if ($db->pe_insert('user', pe_dbhold($_p_info))) {
                //cache_write('user');
                pe_success('添加成功!', 'admin.php?mod=user');
            }
            else {
                pe_error('添加失败...');
            }
        }
        $branch = $db->pe_selectall("branch");
        $job = $db->pe_selectall("job");
        $seo = pe_seo($menutitle='添加用户', '', '', 'admin');
        include(pe_tpl('user_add.html'));
        break;
    //#####################@ 修改用户 @#####################//
    case 'edit':
        $user_id = intval($_g_id);
        if (isset($_p_pesubmit)) {
            pe_token_match();
            $_p_user_pass && $_p_info['user_pass'] = md5($_p_user_pass);
            if ($db->pe_update('user', array('user_id'=>$user_id), pe_dbhold($_p_info))) {
                //cache_write('user');
                pe_success('修改成功!', $_g_fromto);
            }
            else {
                pe_error('修改失败...');
            }
        }
        $branch = $db->pe_selectall("branch");
        $job = $db->pe_selectall("job");
        $info = $db->pe_select('user', array('user_id'=>$user_id));
        $seo = pe_seo($menutitle='修改用户', '', '', 'admin');
        include(pe_tpl('user_add.html'));
        break;
    //#####################@ 用户删除 @#####################//
    case 'del':
        pe_token_match();
        if ($db->pe_delete('user', array('user_id'=>is_array($_p_user_id) ? $_p_user_id : $_g_id))) {
            pe_success('删除成功!');
        }
        else {
            pe_error('删除失败...');
        }
        break;
    //#####################@ 用户导入  @#####################/
    case 'output':
                ob_end_clean();
                header("Content-Type:charset=UTF-8");
                $info_list = $db -> pe_selectall('phone');
                pe_lead('PHPExcel.php');

                // 创建Excel文件对象
                $objPHPExcel = new PHPExcel();
                //获取当前活动的sheet对象
                $objSheet = $objPHPExcel -> getActiveSheet();
                //给当前活动sheet设置名称
                $objSheet -> setTitle('手机列表');
                //给当前活动sheet填充数据
                $objSheet -> setCellValue('A1', '手机id')
                        -> setCellValue('B1', '手机名称')
                        -> setCellValue('C1', '手机价格');
                $index = 2;
                //取得数据库数据
                    foreach ($info_list as $k => $v) {      
                        $objSheet -> setCellValue('A' . $index, $v['phone_id'])
                                 -> setCellValue('B' . $index, $v['phone_name'])
                                 -> setCellValue('C' . $index, $v['phone_price']);
                        $index++;
                    }
                function browser_export($type, $filename) {
                    if ($type == 'Excel3') {
                        //输出EXCEL2003格式
                        header('Content-Type:application/vnd.ms-excel');
                    } else {
                        //输出EXCEL2007格式
                        header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                    }
                    //设置文件名
                    header('Content-Disposition:attachment;filename="' . $filename . '"');
                    //禁止缓存
                    header('Cache-Control:max-age=0');
                }
                
                // 保存Excel 2007格式文件，保存路径为当前路径，名字为export.xlsx
                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                browser_export('Excel2007', '产品价格列表.xlsx');
                $objWriter -> save('php://output');
                exit ;
        # code...
    break;
    case 'input':
        if (isset($_p_pesubmit)) {
            set_time_limit(0);

            //判断是否选择了要上传的表格
            if (empty($_POST['excel'])) {
                echo "<script>alert(您未选择表格);history.go(-1);</script>";
            }

            //获取表格的大小，限制上传表格的大小5M
            $file_size = $_FILES['excel']['size'];
            if ($file_size>5*1024*1024) {
                echo "<script>alert('上传失败，上传的表格不能超过5M的大小');history.go(-1);</script>";
                exit();
            }

            //限制上传表格类型
            $file_type = $_FILES['excel']['type'];
            //application/vnd.ms-excel  为xls文件类型
//			if ($file_type!='application/vnd.ms-excel') {
//			    echo "<script>alert('上传失败，只能上传excel2003的xls格式!');history.go(-1)</script>";
//			 exit();
//			}

            //判断表格是否上传成功
            if (is_uploaded_file($_FILES['excel']['tmp_name'])) {
                require_once 'PHPExcel.php';
                require_once 'PHPExcel/IOFactory.php';
                require_once 'PHPExcel/Reader/Excel5.php';
                //以上三步加载phpExcel的类
                $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
                //接收源文件路径，判断格式
                $filename = $_POST['filepath'];
                //接收存在缓存中的excel表格
                $file = $_FILES['excel']['tmp_name'];
                $extension_arr = explode(".", $_FILES['excel']['name']);
                $extension = $extension_arr[1];
                if ($extension =='xlsx') {
                    $objReader = new PHPExcel_Reader_Excel2007();
                    $objPHPExcel = $objReader ->load($file);
                } else if ($extension =='xls') {
                    $objReader = new PHPExcel_Reader_Excel5();
                    $objPHPExcel = $objReader ->load($file);
                } else if ($extension=='csv') {
                    $PHPReader = new PHPExcel_Reader_CSV();
                    //默认输入字符集
                    $PHPReader->setInputEncoding('GBK');
                    //默认的分隔符
                    $PHPReader->setDelimiter(',');
                    //载入文件
                    $objPHPExcel = $PHPReader->load($file);
                }else{
                    pe_error("格式错误！");
                }

                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                //循环读取excel表格,读取一条,插入一条
                //j表示从哪一行开始读取  从第二行开始读取，因为第一行是标题不保存
                //$a表示列号

                /*
                 * 导入业绩
                 */
                for($j=2;$j<=$highestRow;$j++)
                {
                    //sleep(5);
                    $date = $_p_date."-00";
                    //$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A(名称)列的值
                    $b = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取B(工号)列的值
                    $c = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取C(姓名)列的值
                    $d = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取D(公司)列的值
                    $db->pe_update('phone',array('phone_id'=>$b),array('phone_name'=>$c,'phone_price'=>$d));

                    // $result = 0;

                    // if(strlen($highestColumn)==2){
                    //     $last = (ord($highestColumn[0])-64)*26+ord($highestColumn[1]);
                    // }else{
                    //     $last = ord($highestColumn);
                    // }
                    // for ($i=ord('I'); $i<=$last; $i++){
                    //     if($i>90){
                    //         $row = chr(floor(($i-90)/26)+65).chr(fmod(($i-90),26)+65);
                    //     }else{
                    //         $row = chr($i);
                    //     }
                    //     $jfbz = $objPHPExcel->getActiveSheet()->getCell($row."4")->getValue();//获取E(部门)列的值
                    //     $dfcs = $objPHPExcel->getActiveSheet()->getCell($row."5")->getValue();//获取F(职位)列的值
                    //     $result += ($objPHPExcel->getActiveSheet()->getCell($row.$j)->getValue())*$jfbz*$dfcs;
                    // }

                    // if($c!=""){
                    //     $sql = "INSERT INTO bear_zsf VALUES(null,'$date','$b','$c','$d','$e','$f','content','$result')";
                    //     $res = $db->sql_insert($sql);
                    // }else{
                    //     $res = TRUE;
                    // }

                    ob_flush();//刷新输出缓冲
                    flush();
                }
                $res = true;
                /*
                 * 导入所有部门
                 */
//				for($j=6;$j<=$highestRow;$j++)
//			    {
//			        $e = $objPHPExcel->getActiveSheet()->getCell("E".$j)->getValue();//获取E(状态)列的值
//			        if($db->pe_select('branch',array('branch_name'=>$e))){
//
//			        }else{
//			        	$sql = "INSERT INTO bear_branch VALUES(null,'$e')";
//			        	$res = $db->sql_insert($sql);
//			        }
//					ob_flush();//刷新输出缓冲
//					flush();
//			    }
//               
                if ($res){
                    pe_success("导入成功！");
                }else{
                    pe_error("导入失败！");
                    exit();
                }
            }
        }
        $seo = pe_seo($menutitle='导入用户', '', '', 'admin');
        include(pe_tpl('zsf_add.html'));
        break;
    //#####################@ 重算季度折算分列表 @#####################//
    case 'recal':
        rejiduzsf($_g_year, $_g_jidu);
        break;
    //#####################@ 季度折算分列表 @#####################//
    case 'year':
        set_time_limit(0);
        $nowdate = date("Y",time());
        if($jidu[date("m",time())] == '一'){
            $nowjidu = "01";
        }elseif($jidu[date("m",time())] == '二'){
            $nowjidu = "02";
        }elseif($jidu[date("m",time())] == '三'){
            $nowjidu = "03";
        }elseif($jidu[date("m",time())] == '四'){
            $nowjidu = "04";
        }
        if($_g_jidu){
            if($_g_date){
                jiduzsf($_g_date, $_g_jidu);
            }else{
                jiduzsf($nowdate, $_g_jidu);
            }
        }else{
            jiduzsf($nowdate, $nowjidu);
        }

        $_g_name && $sqlwhere .= " and jiduzsf_user_name like '%{$_g_name}%'";
        $_g_code && $sqlwhere .= " and jiduzsf_user_code like '%{$_g_code}%'";
        $_g_branch && $sqlwhere .= " and jiduzsf_user_branch = '{$_g_branch}'";
        $_g_job && $sqlwhere .= " and jiduzsf_user_job = '{$_g_job}'";
        $_g_company && $sqlwhere .= " and jiduzsf_user_company = '{$_g_company}'";
        if($_g_jidu){
            if($_g_date){
                $sqlwhere .= " and jiduzsf_date = '{$_g_date}-{$_g_jidu}-00'";
            }else{
                $sqlwhere .= " and jiduzsf_date = '{$nowdate}-{$_g_jidu}-00'";
            }
        }else{
            $sqlwhere .= " and jiduzsf_date = '{$nowdate}-{$nowjidu}-00'";
        }
        $sqlwhere .= " order by jiduzsf_id desc";
        $info_list = $db->pe_selectall('jiduzsf', $sqlwhere, '*', array(20, $_g_page));
        $branch = $db->pe_selectall("branch");
        $job = $db->pe_selectall("job");
        $company = $db->pe_selectall("company");
        $seo = pe_seo($menutitle='季度工作业绩折算分列表', '', '', 'admin');
        include(pe_tpl('zsf_list.html'));
        break;
    //#####################@ 工作业绩折算分列表 @#####################//
    default:
//        $nowdate = date("Y-m",time());
//        $_g_name && $sqlwhere .= " and zsf_user_name like '%{$_g_name}%'";
//        $_g_code && $sqlwhere .= " and zsf_user_code like '%{$_g_code}%'";
//        $_g_branch && $sqlwhere .= " and zsf_user_branch = '{$_g_branch}'";
//        $_g_job && $sqlwhere .= " and zsf_user_job = '{$_g_job}'";
//        $_g_company && $sqlwhere .= " and zsf_user_company = '{$_g_company}'";
//        if($_g_date){
//            $sqlwhere .= " and zsf_date = '{$_g_date}-00'";
//        }else{
//            $sqlwhere .= " and zsf_date = '{$nowdate}-00'";
//        }
//        $sqlwhere .= " order by zsf_id desc";
//        $info_list = $db->pe_selectall('zsf', $sqlwhere, '*', array(20, $_g_page));
//        $branch = $db->pe_selectall("branch");
//        $job = $db->pe_selectall("job");
//        $company = $db->pe_selectall("company");
//        $seo = pe_seo($menutitle='工作业绩折算分列表', '', '', 'admin');
        $price = $db->pe_selectall('phone');
        include(pe_tpl('price_list.html'));
        break;
}
?>